package com.example.zs.appbarlayouttest;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.zs.appbarlayouttest.event.RecyclerViewEvent;
import com.example.zs.appbarlayouttest.event.SwipeEnableEvent;
import com.example.zs.appbarlayouttest.listener.OnCurrentRvListener;
import com.example.zs.appbarlayouttest.util.DensityUtil;
import com.example.zs.appbarlayouttest.util.RecyclerViewLocation;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends FragmentActivity {

    @Bind(R.id.tv_main_title1)
    TextView mTvTitle1;
    @Bind(R.id.tv_main_title2)
    TextView mTvTitle2;
    @Bind(R.id.appbarlayout_main)
    AppBarLayout mAblAppBar;
    @Bind(R.id.vp_main)
    ViewPager viewPager;
    @Bind(R.id.rl_main)
    RelativeLayout mLayout;
    @Bind(R.id.swipe_refresh_main)
    SwipeRefreshLayout swipeRefreshView;

    private List<Fragment> fgList;

    private RecyclerViewFragment mFragment;

    private RecyclerViewFragment2 mFragment2;

    private boolean mFlag;

    private AppBarOffsetChangedListener mAppBarListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        fgList = new ArrayList<>();
        mFragment = new RecyclerViewFragment();
        mFragment2 = new RecyclerViewFragment2();
        fgList.add(mFragment);
        fgList.add(mFragment2);

        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fgList.get(position);
            }

            @Override
            public int getCount() {
                return fgList.size();
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mFlag = true;

                // TODO 如果有控件显示消失操作，AppBarLayout.OnOffsetChangedListener才能触发
                mTvTitle1.setVisibility(View.GONE);
                mTvTitle2.setVisibility(View.GONE);
                switch (position) {
                    case 0:
                        mAppBarListener.setListener(mFragment);
                        mTvTitle1.setTextColor(getResources().getColor(R.color.c_1));
                        mTvTitle2.setTextColor(getResources().getColor(R.color.color_black));
                        mTvTitle1.setVisibility(View.VISIBLE);
                        mTvTitle2.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        mAppBarListener.setListener(mFragment2);
                        mTvTitle1.setTextColor(getResources().getColor(R.color.color_black));
                        mTvTitle2.setTextColor(getResources().getColor(R.color.c_1));
                        mTvTitle1.setVisibility(View.VISIBLE);
                        mTvTitle2.setVisibility(View.VISIBLE);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        swipeRefreshView.setColorSchemeResources(R.color.systemcolor);
        swipeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                switch (viewPager.getCurrentItem()) {
                    case 0:
                        ((RecyclerViewFragment) fgList.get(0)).updateData();
                        break;
                    case 1:
                        ((RecyclerViewFragment2) fgList.get(1)).updateData();
                        break;
                }
            }
        });
        swipeRefreshView.setEnabled(false);
        mAppBarListener = new AppBarOffsetChangedListener();
        mAppBarListener.setListener(mFragment);
        mAblAppBar.addOnOffsetChangedListener(mAppBarListener);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setSwipe(SwipeEnableEvent event) {
        if (event.isMsg()) {
            swipeRefreshView.setRefreshing(true);
        } else {
            swipeRefreshView.setRefreshing(false);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setSwipe(RecyclerViewEvent event) {
        if (event.isTop() && mAppBarListener != null) {
            mAppBarListener.onOffsetChanged(mAblAppBar, mAblAppBar.getTop());
        }
    }

    @OnClick({R.id.tv_main_title1, R.id.tv_main_title2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_main_title1:
                viewPager.setCurrentItem(0);
                break;
            case R.id.tv_main_title2:
                viewPager.setCurrentItem(1);
                break;
        }
    }

    class AppBarOffsetChangedListener implements AppBarLayout.OnOffsetChangedListener {

        private OnCurrentRvListener listener;

        public void setListener(OnCurrentRvListener listener) {
            this.listener = listener;
        }

        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

            Log.d("My_Scroll","verticalOffset = " + verticalOffset);
            verticalOffset = Math.abs(verticalOffset);
            int maxScroll = appBarLayout.getTotalScrollRange();
            float percentage = (float) Math.abs(verticalOffset) / (float) maxScroll;
            if (!mFlag) {
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                if (percentage > 0 && percentage < 1) {
                    int height = (int) (DensityUtil.dip2px(MainActivity.this, 60) - DensityUtil.dip2px(MainActivity.this, 20) * percentage);
                    params.addRule(RelativeLayout.CENTER_HORIZONTAL);
                    params.height = height;
                    params.width = height;
                    params.topMargin = (int) (DensityUtil.dip2px(MainActivity.this, 20) - DensityUtil.dip2px(MainActivity.this, 15) * percentage);
                    mLayout.setLayoutParams(params);
                }
            } else {
                mFlag = false;
            }

            if (verticalOffset == 0 && listener != null && RecyclerViewLocation.isScrollTop(listener.getRecyclerView())) {
                swipeRefreshView.setEnabled(true);
            } else {
                swipeRefreshView.setEnabled(false);
            }
        }
    }

}
